#include "functions.h"

char * num_str(register double value, register char type)
{
	char * string = (char*)calloc((size_t)log10(value) + sizeof(char),sizeof(char));
	if (!type)
		sprintf(string, "%ld", (long long)value);
	else
		if (string != NULL)
			sprintf(string, "%lf", value);
	return string;
}
double str_num(char * string)
{
	bool minus = false;
	if (string[0] == '-')
		minus = true;
	char *token = strchr(string, '.');
	double result = 0;
	if (token != NULL)
		{
			for (int i = minus ? 1 : 0; i < token - string; i++, result *= 10 )
				result += string[i] - '0';
			result /= 10;
			double posfix = 0;
			for (int i = (int)strlen(token) - 1; i > 0; i--, posfix /= 10)
				posfix += token[i] - '0';
			result += posfix;
			result *= 10;
		}
	else
		for (int i = minus ? 1 : 0; i < (int)strlen(string); i++, result *= 10)
			result += string[i] - '0';
	return minus ? -result/10 : result/10; 
}
char * line_input(void)
{
	register char ch = 1;

	char * string = (char*)calloc(sizeof(char), sizeof(char) * 4);

	while ((ch = getchar()) != '\n')
	{
		strcat(string, chrstr(ch));
		string = (char*)realloc(string, sizeof(char) * (strlen(string) + 4));
	}
	return string;
}
char * strrep(register char * string, register char * target, register char * value)
{
	while (strstr(string, target) != NULL)
	{
		register int start = strstr(string, target) - string;
		register char * front = strmid(string, 0, start);
		register char * tail = strmid(string, start + strlen(target), strlen(string));
		front = (char*)realloc(front, sizeof(char) + sizeof(char) * (strlen(front) + strlen(value) + strlen(tail)));
		strcat(front, value);
		strcat(front, tail);
		string = front;
	}
	return string;
}
char * strsep(register char **string, register const char *delim, register char * delimReturn)
{
	register char *s;
	register const char *spanp;
	register int c, sc;
	char *tok;

	if ((s = *string) == NULL)
		return (NULL);

	for (tok = s;;)
	{
		c = *s++;
		spanp = delim;
		do {
			if ((sc = *spanp++) == c)
			{
				if (!c) s = NULL;
				else s[-1] = 0;
				*string = s;
				if (delimReturn != NULL)
					*delimReturn = c;
				return (tok);
			}
		} while (sc);
	}
}
char * chrstr(register char ch)
{
	register char * string = (char*)malloc(sizeof(char) * 2);
	string[0] = ch;
	string[1] = 0;
	return string;
}
char * strmid(register char * string, register int start, register int len)
{
	register char * str = (char *)malloc(sizeof(char) * strlen(string));
	if (start > (int)strlen(string)) return "";
	for (register int i = start; (i < len + start) && strlen(string); i++)
		str[i - start] = string[i];
	if (len < 0) return "";
	else str[len] = 0;
	return str;
}
char * trim(register char * string)
{
	char * temp = (char*)calloc(strlen(string) + 1, sizeof(char));
	for (int i = 0, j = 0; i < (int)strlen(string); i++)
		if (string[i] != ' ')
			temp[j++] = string[i];
	strcpy(string, temp);
	delete temp;
	return string;
}
char * trimLR(register char * string, register int type)
{
	for (int i = 0; i < (int)strlen(string); i++)
	{
		if (string[i] != ' ' && !type)
			return strmid(string, i, strlen(string) - i - 1);
		if (string[strlen(string) - i - 1] != ' ' && type)
			return strmid(string, 0, strlen(string) - i);
	}
	return string;
}
char typeof(register char * string)
{
	for (register int i = 0; i < (int)strlen(string); i++)
	{
		if (string[i] == '\'')
			return 2;
		else if (string[i] == '.')
			return 1;
		else if ((string[i] < '0' || string[i] > '9') && string[i] !='-' )
			return 3;
	}
	return 0;
}
